/**
 * Description:该类主要用于创建一个平铺选择框
 * Copyright: 成都和为科技有限公司 2012
 *
 * 属性：
 *   datasource - 数据资源.对应服务器端action
 *   type -  数据类型，默认为code.自动加上valuetag,displaytag,submittag. 可选data
 *   coderes -  当type为code使用，传编码类型
 *   describle - 下拉列表描述
 *   valuetag - 当数据资源为DataService时,选中值对应DataService结果的列名
 *   displaytag - 当数据资源为DataService时, 显示值对应DataService结果的列名
 *   submittag - 除了提交valuetag到服务器，还会组装submittag内容到服务器. 单选可用
 *      如:path="sex", submittag="{'name':'sexname'}"  服务器端接收到的数据为sex:男, sexname:1
 *   check - 是否支持多选, 默认为false
 *   load - 是否初始化加载值.默认为true 方法
 *
 * 事件:
 *  initdata - 下拉列表数据加载前调用
 *  onload - 下拉列表数据加载完成调用事件
 *  onchange - 改变值
 */
function DataBox() {
}

DataBox.inherit(Input);

/**
 * 初始化方法
 */
DataBox.prototype.init = function() {
    DataBox.base.init.call(this);
}

/**
 * 创建HTML节点
 */
DataBox.prototype.create = function() {
    this._html = document.createElement("div");
    $(this._html).addClass("control-databox");
}

DataBox.prototype.initLoad = function() {
    DataBox.base.initLoad.call(this);
    if (this.getAttribute().params) {
        var sParam = this.getAttribute().params.replaceAll("'", "\"");
        var oParam = JSON.parse(sParam);
        this._queryParam = oParam;
    } else {
        this._queryParam = new Object();
    }
    if (!this.getAttribute().type || this.getAttribute().type == "code") {
        if (!this.getAttribute().type)
            this.getAttribute().type = "code";
        if (!this.getAttribute().displaytag)
            this.getAttribute().displaytag = "cn";
        if (!this.getAttribute().valuetag)
            this.getAttribute().valuetag = "cn";
        if (!this.getAttribute().submittag)
            this.getAttribute().submittag = "{'" + this.getAttribute().path
                + "dm':'dm'}";
        this._queryParam.type = this.getAttribute().coderes;
    }
    this._load = false;
    if (this.getAttribute().load != "false") {
        this.loadData();
    }
}

/**
 * 加载数据
 * @param {} oParam
 */
DataBox.prototype.loadData = function() {
    if (!this.getDataSource()) return;
    var that = this;
    var oReq = new Object();
    oReq.params = this._queryParam;
    this.getDataSource().queryData(oReq, true, function(oResult) {
        $(that.getHTML()).empty();
        that.triggerAttributeEvent("initdata", oResult.records);
        that.showData(oResult.records);
        //绑定点击事件
        $(".databox-item", that.getHTML()).bind("click", function(e){
            that.clickItem(this);
        });
        that._load = true;
        that.onload();
        that.triggerAttributeEvent("onload", oResult.records);
        if (that._attr.disabled == "true") {
            that.setDisable();
        }
    });
}

/**
 * 建立数据
 *
 * @param {} oResult 结果值
 * @param {} valueTag 值标识
 * @param {} displayTag 显示标识
 */
DataBox.prototype.showData = function(records) {
    var valueTag = this.getAttribute().valuetag;
    var displayTag = this.getAttribute().displaytag;
    for ( var i = 0; i < records.length; i++) {
        var record = records[i];
        var itemDom = $("<span class='databox-item'></span>");
        $(itemDom).text(record[displayTag]);
        $(itemDom)[0].result = record;
        $(itemDom).attr("value", record[valueTag]);
        $(this._html).append(itemDom);
    }
}

/**
 * 如果未加载完成设置了值的,等加载完成再设置值.
 */
DataBox.prototype.onload = function() {
    if (this._setValue) {
        this.setValue(this._setValue);
        this._setValue = null;
    }
}

/**
 * 点击元素
 * @param clickDom
 */
DataBox.prototype.clickItem = function(clickDom) {
    if (this.getAttribute().check == "true") {
        $(clickDom).addClass("databox-item-active");
        this._result = [];
        var selectDoms = $(".databox-item-active", this.getHTML());
        for (var i = 0 ; i < selectDoms.length ; i++) {
            this._result.push(selectDoms[i].result);
        }
    } else {
        $(".databox-item", this.getHTML()).removeClass("databox-item-active");
        $(clickDom).addClass("databox-item-active");
        this._result = clickDom.result;
    }
    this.setValidate(true);
    this.triggerAttributeEvent("onchange", this._result);
    this.triggerAttributeEvent("validate", this._result);
    this.showError();
}

/**
 * 根据查询条件重建立下拉列表
 * @param {} oParam
 */
DataBox.prototype.reBuild = function(oParam) {
    if (oParam)
        this._queryParam = oParam;
    this.loadData();
}

/**
 * 获得选择结果
 * @return {}
 */
DataBox.prototype.getResult = function() {
    return this._result;
}

/**
 * 设置数据集
 * @param {} oData 数组对象
 */
DataBox.prototype.setData = function(oData) {
    this.getDataSource().setData(oData);
    this.loadData();
}

/**
 * 获得选择结果
 * @return {}
 */
DataBox.prototype.getResult = function() {
    return this._result;
}

/**
 * 绑定当前控件值
 */
DataBox.prototype.setValue = function(value, oData) {
    if (!value || value == "") {
    	if (this.getAttribute().check == "true") {
    		this._result = [];
    	} else {
    	 	this._result = new Object();
    	}
    	return;
    }
    //未加载完成数据，等加载完成回调设值
//    if (!this._load) {
        this._setValue = value;
//    }
    var itemDoms = $(".databox-item", this.getHTML());
    if (this.getAttribute().check == "true") {
        this._result = [];
        var values = value.split(",")
        for (var i = 0 ; i < values.length ; i++) {
            for (var j = 0 ; j < itemDoms.length ; j++) {
                var domValue = $(itemDoms[j]).attr("value");
                if (values[i] == domValue) {
                    $(itemDoms[j]).addClass("databox-item-active");
                    this._result.push(itemDoms[j].result);
                }
            }
        }
    } else {
        for (var i = 0 ; i < itemDoms.length ; i++) {
            var domValue = $(itemDoms[i]).attr("value");
            if (value == domValue) {
                $(itemDoms[i]).addClass("databox-item-active");
                this._result = itemDoms[i].result;
            }
        }
    }
}

DataBox.prototype.getValue = function(oData) {
    var itemActiveDoms = $(".databox-item-active", this.getHTML());
    if (this.getAttribute().check == "true") {
        var value = [];
        for (var i = 0 ; i < itemActiveDoms.length ; i++) {
            value.push($(itemActiveDoms[i]).attr("value"));
        }
        value = value.join(",");
    } else {
        var value = "";
        if (itemActiveDoms.length > 0) {
            value = $(itemActiveDoms[0]).attr("value");
            this.buildSubmit(oData);
        }
    }
    return value;
}

/**
 * 设置为可编辑
 */
DataBox.prototype.setEnable = function() {
    var that = this;
    $(".databox-item", this.getHTML()).bind("click", function(e){
        that.clickItem(this);
    });
    this._disable = false;
}

/**
 * 设置为只读
 */
DataBox.prototype.setDisable = function() {
    $(".databox-item", this.getHTML()).unbind("click");
    this._disable = true;
}
